package com.server.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.common.code.error.BusinessException;
import com.server.model.all.user.bo.RoleAddBo;
import com.server.model.all.user.entity.RoleEntity;
import com.server.user.mapper.RoleMapper;
import com.server.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Resource
    private RoleMapper roleMapper;


    @Override
    public Integer roleAdd(RoleAddBo roleAddBo) {
        //判断是否存在角色key或者角色名
        boolean exists = roleMapper.exists(new LambdaQueryWrapper<RoleEntity>()
                .eq(RoleEntity::getRoleKey, roleAddBo.getRoleKey())
                .or()
                .eq(RoleEntity::getRoleName,roleAddBo.getRoleName())
        );

        //是否存在
        if(exists){
            throw new BusinessException("已存在该角色");
        }

        //新增角色
        return roleMapper.insert(RoleEntity.builder()
                        .createUserId(roleAddBo.getCreateUserId())
                        .remarks(roleAddBo.getRemarks())
                        .roleName(roleAddBo.getRoleName())
                        .roleKey(roleAddBo.getRoleKey())
                .build());
    }
}
